package com.agil.photoalbum.util;

import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Thumbnail {
	
	private static final Log log = LogFactory.getLog(Thumbnail.class);
	
	private static final int THUMBNAIL_MAX_WIDTH = 100;
	private static final int THUMBNAIL_MAX_HEIGHT = 100;
	private static final float QUALITY = 0.75f;
	
	byte [] thumbStream;
	private Image image;
	private int thumbWidth;
	private int thumbHeight;
	
	public void generateThumbnail(String picturePath){
		loadPicture(picturePath);
		try {
			drawThumbnail();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void loadPicture(String picturePath){
		image = Toolkit.getDefaultToolkit().getImage(picturePath);
		MediaTracker mediaTracker = new MediaTracker(new Container());
		mediaTracker.addImage(image, 0);
		try {
			mediaTracker.waitForID(0);
		} catch (InterruptedException e) {
			log.error("Error while loading image");
		}
	}
	
	private void drawThumbnail() throws FileNotFoundException{
				
		setCurrentThumbnailSize();
		
		BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2D = thumbImage.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
		
		FileOutputStream out = new FileOutputStream("bmp_tmb.jpg");
		
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
		
		param.setQuality(QUALITY, false);
		encoder.setJPEGEncodeParam(param);
		try {
			encoder.encode(thumbImage);
//			thumbStream= out.toByteArray();
			out.flush();
		} catch (ImageFormatException e) {
			log.error("Image format not valid.");
		} catch (IOException e) {
			log.error("Could not encode thumbnail");
		}
	}
	
	private void setCurrentThumbnailSize(){
		thumbWidth = THUMBNAIL_MAX_WIDTH;
		thumbHeight = THUMBNAIL_MAX_HEIGHT;
		double thumbRatio = (double) thumbWidth / (double) thumbHeight;
		int imageWidth = image.getWidth(null);
		int imageHeight = image.getHeight(null);
		double imageRatio = (double) imageWidth / (double) imageHeight;
		if (thumbRatio < imageRatio) {
			thumbHeight = (int) (thumbWidth / imageRatio);
		} else {
			thumbWidth = (int) (thumbHeight * imageRatio);
		}
	}
	
	public byte[] getThumbStream() {
		return thumbStream;
	}

	public void setThumbStream(byte[] thumbStream) {
		this.thumbStream = thumbStream;
	}	
}
